using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;

namespace Bots.Index
{
    sealed public class ConvertorRegistry
    {
        static ConvertorRegistry registry;
        private Hashtable hash = new Hashtable();
        private bool isInit = false;
        private ConvertorRegistry() { }

        public static ConvertorRegistry Instance {
            get {
                if (registry == null) {
                    registry = new ConvertorRegistry();
                }
                return registry;
            } 
        }

        public IConvertor Find(DomainObject domainObject) {
            return hash[domainObject.GetType().Name] as IConvertor;    
        }

        public void Init(NameValueCollection convertors) {
            if (isInit) return;
            for (int i = 0; i < convertors.Count; i++) {
                IConvertor convertor = Activator.CreateInstance(Type.GetType(convertors[i])) as IConvertor;
                AddConvertor(convertors.Keys[i], convertor);
            }
            isInit = true;
        }

        public void AddConvertor(string name, IConvertor convertor)
        { 
            if(!hash.Contains(name)){
                hash.Add(name, convertor);
            }
        }
    }
}
